import 'package:aichat_flutter_wallet/module_common/provider/nav_key.dart';
import 'package:aichat_flutter_wallet/module_common/provider/wallet_info_provider.dart';
import 'package:flutter/material.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';
import 'package:lib_wallet_connect/utils/chain_data.dart';
import 'package:lib_wallet_connect/utils/chain_metadata.dart';
import 'package:lib_web3/lib_solana_web3.dart';
import 'package:lib_web3/lib_tron_web3.dart';
import 'package:lib_web3/lib_web3_method.dart';

class ChainListTool {
  static ChainType callBackChainType(int chainId) {
    if ([95279725].contains(chainId)) {
      return ChainType.sol;
    }
    if ([
      3448148188,
      728126428,
    ].contains(chainId)) {
      return ChainType.tron;
    }
    return ChainType.eth;
  }

  static String callbackDerivePath(int chainId) {
    if ([95279725].contains(chainId)) {
      return ChainList.derivePathSol;
    }
    if ([
      3448148188,
      728126428,
    ].contains(chainId)) {
      return ChainList.derivePathTRON;
    }
    return ChainList.derivePathEVM;
  }

  static setWCChainList(List<Chain> list) {
    List<ChainMetadata> result = [];
    for (var i = 0; i < list.length; i++) {
      Chain group = list[i];
      if ([728126428, 95279725].contains(group.evmInfo?.chainId)) {
        continue;
      }
      result.add(ChainMetadata(
        type: WCChainType.eip155,
        chainId: 'eip155:${group.evmInfo?.chainId.toString()}',
        name: group.name,
        logo: group.logo,
        color: Colors.blue.shade300,
        rpc: group.evmInfo!.rpcList,
      ));
    }
    ChainData.allChains = result;
  }

  //  返回 主流币 精度， evm
  static callBackDecimals(String symbol) {
    if (symbol.toUpperCase() == 'SOL') {
      return 9;
    }
    if (symbol.toUpperCase() == 'TRX' || symbol.toUpperCase() == 'TRON') {
      return 8;
    }
    return 18;
  }

  static getMainTokenBalance(int chainId, String rpcUrl, String userAddress,
      {int decimal = 18}) {
    if ([95279725].contains(chainId)) {
      return SolanaWeb3.getMainTokenBalance(rpcUrl, userAddress);
    }
    if ([
      3448148188,
      728126428,
    ].contains(chainId)) {
      return TronWeb3.getMainTokenBalance(rpcUrl, userAddress);
    }
    return FlutterWeb3.getMainTokenBalance(rpcUrl, userAddress);
  }

  static getTokenBalance(String rpc, String tokenAddress, String userAddress,
      {int decimals = 18, required int chainId}){
    if ([95279725].contains(chainId)) {
      return SolanaWeb3.getTokenBalance(rpc,
        tokenAddress,
        userAddress,
        decimals: decimals,);
    }
    if ([
      3448148188,
      728126428,
    ].contains(chainId)) {
      return TronWeb3.getTokenBalance(rpc,
        tokenAddress,
        userAddress,
        decimal: decimals,);
    }
    return FlutterWeb3.getTokenBalance(
      rpc,
      tokenAddress,
      userAddress,
      decimals: decimals,);
  }

  //  返回 主流币 精度， evm
  static getERC20TokenInfoByRpc(
      {required String rpc,
      required String tokenAddress,
      required String symbol}) {
    final userAddress =
        NavKey.getContext().read<WalletInfoNotifier>()
            .walletAddress;
    if (symbol.toUpperCase() == 'SOL' || symbol.toUpperCase() == 'SOLANA'|| symbol.toUpperCase() == 'SOLNAN') {
      return SolanaWeb3.getERC20TokenInfoByRpc(rpc,
          accountAddress: userAddress, tokenMintAddress: tokenAddress);
    }
    if (symbol.toUpperCase() == 'TRX' || symbol.toUpperCase() == 'TRON') {
      return TronWeb3.getERC20TokenInfoByRpc(rpc,
          userAddress: userAddress, tokenAddress: tokenAddress);
    }
    return FlutterWeb3.getERC20TokenInfoByRpc(rpc, tokenAddress);
  }

  //  返回 主流币 精度， evm
  static getErc20Gas(
      {required String rpcUrl,
      required String transaction,
      required String symbol}) {
    if (symbol.toUpperCase() == 'SOL' || symbol.toUpperCase() == 'SOLANA'|| symbol.toUpperCase() == 'SOLNAN') {
      return SolanaWeb3.getErc20Gas(rpcUrl,transaction);
    }
    if (symbol.toUpperCase() == 'TRX' || symbol.toUpperCase() == 'TRON') {
      return TronWeb3.getErc20Gas(rpcUrl,transaction);
    }
    return FlutterWeb3.getErc20Gas(rpcUrl, transaction);
  }

  //  转账 签名
  static getWeb3SignTx(rpc, chainId, tran, contract, type) {
    if ([95279725].contains(chainId)) {
      return SolanaWeb3.getWeb3SignTx(rpc, chainId, tran, contract, type);
    }
    if ([
      3448148188,
      728126428,
    ].contains(chainId)) {
      return TronWeb3.getWeb3SignTx(rpc, chainId, tran, contract, type);
    }
    return FlutterWeb3.getWeb3SignTx(rpc, chainId, tran, contract, type);
  }

  static ethSignSendTransaction(String rpcUrl, int chainId,txInfo, String signInfo,
      {bool status = false}) {
    if ([95279725].contains(chainId)) {
      return SolanaWeb3.ethSignSendTransaction(
          rpcUrl, chainId, txInfo, signInfo,
          status: status);
    }
    // if ([
    //   3448148188,
    //   728126428,
    // ].contains(chainId)) {
    //   return TronWeb3.ethSignSendTransaction(
    //       rpcUrl, chainId, txInfo, signInfo,
    //       status: status);
    // }
    return FlutterWeb3.ethSignSendTransaction(
        rpcUrl, chainId, txInfo, signInfo,
        status: status);
  }
}
